package com.xuetang9.t9.hotel.controller;


import com.xuetang9.t9.hotel.beans.Reservation;
import com.xuetang9.t9.hotel.mapper.condition.impl.ReservationCondition;
import com.xuetang9.t9.hotel.services.IReservationService;
import com.xuetang9.t9.hotel.utils.HttpCodeEnum;
import com.xuetang9.t9.hotel.utils.PageBean;
import com.xuetang9.t9.hotel.utils.ResponseResult;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 预订controller
 */
@Controller
@RequestMapping("/res")
@ResponseBody
public class ReservationController {

    @Autowired
    private IReservationService reservationService;


    /**
     * 列表查询
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/show",method = RequestMethod.POST)
    private PageBean show(@RequestBody ReservationCondition condition){
        return reservationService.findByAll(condition);
    }


    /**
     * 新添加预订订单
     * @param reservation
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    private ResponseResult save(@RequestBody Reservation reservation){
        if(reservation == null){
            return ResponseResult.okResult(HttpCodeEnum.PARAM_INVALID.getCode(),HttpCodeEnum.PARAM_INVALID.getErrorMessage());
        }
        reservationService.save(reservation);
        return ResponseResult.okResult(HttpCodeEnum.SUCCESS.getCode(),HttpCodeEnum.SUCCESS.getErrorMessage());
    }


    /**
     * 删除订单
     * @param id
     * @return
     */
    @RequestMapping("/remove/{id}")
    private ResponseResult remove(@PathVariable("id") Long id){
        if(id == null){
            return ResponseResult.okResult(HttpCodeEnum.PARAM_INVALID.getCode(),HttpCodeEnum.PARAM_INVALID.getErrorMessage());
        }
        if(reservationService.remove(id)){
            return ResponseResult.okResult(HttpCodeEnum.SUCCESS.getCode(),HttpCodeEnum.SUCCESS.getErrorMessage());
        }
        return  ResponseResult.okResult(HttpCodeEnum.PARAM_INVALID.getCode(),HttpCodeEnum.PARAM_INVALID.getErrorMessage());
    }


    /**
     * 更新预订订单
     * @param reservation
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    private ResponseResult update(@RequestBody Reservation reservation){
        if (null == reservation){
            return ResponseResult.okResult(HttpCodeEnum.PARAM_INVALID.getCode(),HttpCodeEnum.PARAM_INVALID.getErrorMessage());
        }
        if(reservationService.alter(reservation)){
            return ResponseResult.okResult(HttpCodeEnum.SUCCESS.getCode(),HttpCodeEnum.SUCCESS.getErrorMessage());
        }
        return  ResponseResult.okResult(HttpCodeEnum.PARAM_INVALID.getCode(),HttpCodeEnum.PARAM_INVALID.getErrorMessage());
    }

}
